<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customermodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'customer_id' => 'customer.customer_id',
        'customer_status' => 'customer.customer_status',
        'customer_rental_device_type' => 'customer_rental_device_type',
    );
    public static $likeField = array(
      'customer_name' => 'customer_name',
      'customer_phone' => 'customer_phone',
      'customer_email' => 'customer_email',
      'customer_companey' => 'customer_companey',
      'customer_time_create_start' => 'customer_time_create >=',
      'customer_time_create_end' => 'customer_time_create <=',
      'customer_rental_name' => 'customer_rental_name',
      'customer_rental_companey' => 'customer_rental_companey',
      'customer_rental_position' => 'customer_rental_position',
      'customer_rental_email' => 'customer_rental_email',
      'customer_rental_contract' => 'customer_rental_contract',
      'customer_rental_time_create_start' => 'customer_rental_time_create >=',
      'customer_rental_time_create_end' => 'customer_rental_time_create <=',

    );
    public static $sortField = array();

    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getCustomerListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('customer');
        $this->db->where('customer_status !=',STATUS_DELETE);
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }
        if(!isset($assist['nocount'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        // $this->db->order_by('customer_status','desc');
        // $this->db->order_by('customer_sort','desc');
        $this->db->order_by('customer_id','desc');
        if(isset($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(isset($assist['nocount'])) return $list;
        return array($list,$count);
    }

    public function getCustomerRentalListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('customer_rental');
        // $this->db->where('customer_status !=',STATUS_DELETE);
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }
        if(!isset($assist['nocount'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        // $this->db->order_by('customer_status','desc');
        // $this->db->order_by('customer_sort','desc');
        $this->db->order_by('customer_rental_id','desc');
        if(isset($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(isset($assist['nocount'])) return $list;
        return array($list,$count);
    }

    public function getCustomerSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('customer');
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getCustomer($customer_id){
        $this->db->from('customer');
        $this->db->where('customer_id',$customer_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getCustomerByPhone($customer_phone){
        $this->db->from('customer');
        $this->db->where('customer_phone',$customer_phone);
        $this->db->where('customer_status !=',STATUS_DELETE);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getCustomerById($customer_id){
        if(!is_array($customer_id)) $customer_id = array($customer_id);
        if(empty($customer_id)) return array();

        $this->db->from('customer');
        $this->db->where_in('customer_id',$customer_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }

    public function getCustomerRentalListByCustomerId($customer_id) {
      $this->db->from('customer_rental');
      $this->db->where('customer_id',$customer_id);
      $query = $this->db->get();
      $list = $query->result_array();

      return $list;
    }


/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createCustomer($info){
        $this->db->insert('customer',$info);
        $customer_id = $this->db->insert_id();

        return $customer_id;
    }

    public function createCustomerRental($info){
        $this->db->insert('customer_rental',$info);
        $id = $this->db->insert_id();

        return $id;
    }

    public function createCustomerBatch($info){
        if (!empty($info)) {
            $this->db->insert_batch('customer',$info);
        }
    }

/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
    public function editCustomer($info){
        if(isset($info['customer_id'])) {
            $customer_id = $info['customer_id'];
            unset($info['customer_id']);
            $this->db->where('customer_id',$customer_id);
            $this->db->update('customer',$info);
        }
    }

    public function updateCustomerBatch($update_arr){
        if (!empty($update_arr)) {
            $this->db->update_batch('customer',$update_arr,'customer_id');
        }
    }

}
